/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * 全局线程池
 * Global executor pools for the whole application.
 * 提供系统性能，例如：io操作不需等网络请求后执行
 * <p>
 * 用法：AppExecutors.getInstance().driveWorkThread().execute(() -> {});
 * </p>
 */
public class AppExecutors {

    private static class PlayUtilsHolder {
        private static final AppExecutors instance = new AppExecutors();
    }

    public static AppExecutors getInstance() {
        return PlayUtilsHolder.instance;
    }

    private Executor mainThread;//ui线程操作
    private ExecutorService workThread;//工作线程池，执行普通任务。例如：网络通讯和多媒体操作
    private ScheduledExecutorService scheduledWork;//循环线程池，执行普通循环任务。

    private ExecutorService driveWorkThread;//工驾驶界面专用，执行普通任务。例如：网络通讯和多媒体操作
    private ScheduledExecutorService driveScheduledWork;//驾驶界面专用，执行普通循环任务。例如：mqtt心跳、车视频心跳、车音频心跳
    private ScheduledExecutorService driveRockerWork;//驾驶界面专用，循环发送车控制报文。例如：模拟摇杆、usb外接手柄、蓝牙手柄

    public Executor mainThread() {
        if (mainThread == null) {
            mainThread = new MainThreadExecutor();
        }
        return mainThread;
    }

    private static class MainThreadExecutor implements Executor {
        private final Handler mainThreadHandler = new Handler(Looper.getMainLooper());

        @Override
        public void execute(Runnable command) {
            mainThreadHandler.post(command);
        }
    }

    public ExecutorService workThread() {
        if (workThread == null || workThread.isShutdown())
            workThread = Executors.newFixedThreadPool(6);//骁龙888八个CPU核心
        return workThread;
    }

    public ScheduledExecutorService scheduledWork() {
        if (scheduledWork == null || scheduledWork.isShutdown())
            scheduledWork = Executors.newScheduledThreadPool(4);
        return scheduledWork;
    }

    public ExecutorService driveWorkThread() {
        if (driveWorkThread == null || driveWorkThread.isShutdown())
            driveWorkThread = Executors.newFixedThreadPool(6);//骁龙888八个CPU核心
        return driveWorkThread;
    }

    public ScheduledExecutorService driveScheduledWork() {
        if (driveScheduledWork == null || driveScheduledWork.isShutdown())
            driveScheduledWork = Executors.newScheduledThreadPool(6);
        return driveScheduledWork;
    }

    public ScheduledExecutorService driveRockerWork() {
        if (driveRockerWork == null || driveRockerWork.isShutdown())
            driveRockerWork = Executors.newScheduledThreadPool(1);
        return driveRockerWork;
    }

    public void shutdownWorkThread() {
        if (workThread != null) {
            workThread.shutdown();
            workThread = null;
        }
    }

    public void shutdownScheduledWork() {
        if (scheduledWork != null) {
            scheduledWork.shutdown();
            scheduledWork = null;
        }
    }

    public void shutdownDriveWorkThread() {
        if (driveWorkThread != null) {
            driveWorkThread.shutdown();
            driveWorkThread = null;
        }
    }

    public void shutdownDriveScheduledWork() {
        if (driveScheduledWork != null) {
            driveScheduledWork.shutdown();
            driveScheduledWork = null;
        }
    }

    public void shutdownDriveRockerWork() {
        if (driveRockerWork != null) {
            driveRockerWork.shutdown();
            driveRockerWork = null;
        }
    }
}

